﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using SD.BUS;
using SD.Common;
using SD.Entities;
using SD.IBUS;

namespace SD.UI
{
    public partial class ShowOrganizationList : System.Web.UI.Page
    {
        private static string ASCENDING = " ASC";
        private static string DESCENDING = " DESC";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["filterString"] == null) // no filter string param
                {
                    Session.Add("filterString", "");
                }

                if (Session["includeInactive"] == null) // no include inactive param
                {
                    Session.Add("includeInactive", false);
                }
                else
                {
                    chkInActive.Checked = Convert.ToBoolean(Session["includeInactive"]);
                }

                LoadData(Session["filterString"].ToString(), Convert.ToBoolean(Session["includeInactive"]));
            }
        }

        protected void chkInActive_CheckedChanged(object sender, EventArgs e)
        {
            Session["includeInactive"] = chkInActive.Checked;
            LoadData(Session["filterString"].ToString(), chkInActive.Checked);
        }

        /// <summary>
        /// Get all list organization
        /// </summary>
        private void LoadData(String filterString, Boolean includeInactive)
        {
            if (filterString.Equals("")) // show all organization
            {
                IOrganizationBIZ orgBIZ = new OrganizationBIZ();
                DataTable dt = orgBIZ.ListOrganization(includeInactive);
                BindData(dt);
            }
            else
            {
                IOrganizationBIZ orgBIZ = new OrganizationBIZ();
                DataTable dt = orgBIZ.ListFilteredOrganization(filterString, includeInactive);
                BindData(dt);
            }

            Session["filterString"] = filterString;
        }

        /// <summary>
        /// Bind data to datagrid
        /// </summary>
        /// <param name="dtbDataSource"></param>
        private void BindData(DataTable dtbDataSource)
        {
            // If have data in table
            if (dtbDataSource.Rows.Count > 0)
            {
                gvOrganizationList.DataSource = dtbDataSource;
                gvOrganizationList.DataBind();
                lblMessage.Text = string.Empty;
            }
            else // if have no data in table
            {
                dtbDataSource.Rows.Add(dtbDataSource.NewRow());
                gvOrganizationList.DataSource = dtbDataSource;
                gvOrganizationList.DataBind();
                gvOrganizationList.Rows[0].Visible = false;
                lblMessage.Text = "No records found";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;
                return (SortDirection)ViewState["sortDirection"];
            }
            set { ViewState["sortDirection"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sortExpression"></param>
        /// <param name="direction"></param>
        private void SortGridView(string sortExpression, string direction)
        {
            IOrganizationBIZ orgBIZ = new OrganizationBIZ();
            DataTable tb = new DataTable();

            String str = Session["filterString"].ToString();
            // List all organization
            if (String.IsNullOrEmpty(Session["filterString"].ToString()))
            {
                tb = orgBIZ.ListOrganization(Convert.ToBoolean(Session["includeInactive"]));
            }
            // List organization is up to remain filter
            else
            {
                tb = orgBIZ.ListFilteredOrganization(Session["filterString"].ToString(), Convert.ToBoolean(Session["includeInactive"]));
            }
            if (tb.Rows.Count > 0)
            {
                DataView dv = new DataView(tb);
                dv.Sort = sortExpression + direction;
                gvOrganizationList.DataSource = dv;
                gvOrganizationList.DataBind();
            }
            else
            {
                tb.Rows.Add(tb.NewRow());
                gvOrganizationList.DataSource = tb;
                gvOrganizationList.DataBind();
                gvOrganizationList.Rows[0].Visible = false;
                lblMessage.Text = "No records found";
            }
        }

        /// <summary>
        /// Filter: get all list organization
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnAll_Click(object sender, EventArgs e)
        {
            LoadData("", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        /// <summary>
        /// Filter: all organization that begin with the letter in '0 - 9'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbn09_Click(object sender, EventArgs e)
        {
            LoadData("[0-9]%", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        /// <summary>
        /// Filter: all organization that begin with the letter in 'A B C D E'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnABCDE_Click(object sender, EventArgs e)
        {
            LoadData("[A-E]%", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        /// <summary>
        /// Filter: all organization that begin with the letter in 'F G H I J'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnFGHIJ_Click(object sender, EventArgs e)
        {
            LoadData("[F-J]%", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        /// <summary>
        /// Filter: all organization that begin with the letter in 'K L M N'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnKLMN_Click(object sender, EventArgs e)
        {
            LoadData("[K-N]%", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        /// <summary>
        /// Filter: all organization that begin with the letter in 'O P Q R'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnOPQR_Click(object sender, EventArgs e)
        {
            LoadData("[O-R]%", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        /// <summary>
        /// Filter: all organization that begin with the letter in 'S T U V'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSTUV_Click(object sender, EventArgs e)
        {
            LoadData("[S-V]%", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        /// <summary>
        /// Filter: all organization that begin with the letter in 'W X Y Z'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnWXYZ_Click(object sender, EventArgs e)
        {
            LoadData("[W-Z]%", Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddOrganization.aspx");
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {

        }

        protected void gvOrganizationList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrganizationList.PageIndex = e.NewPageIndex;
            LoadData(Session["filterString"].ToString(), Convert.ToBoolean(Session["includeInactive"].ToString()));
        }

        protected void gvOrganizationList_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            ViewState["SortExpression"] = sortExpression;
            if (GridViewSortDirection == SortDirection.Ascending)
            {
                GridViewSortDirection = SortDirection.Descending;
                SortGridView(sortExpression, DESCENDING);
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                SortGridView(sortExpression, ASCENDING);
            }

        }

        protected void gvOrganizationList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "showPopup", "if (!confirm('Do you want to add this author?')) return false;", true);
            string orgId;
            int row = gvOrganizationList.SelectedIndex;
            orgId = gvOrganizationList.Rows[row].Cells[0].Text.ToString();
            Response.Redirect("EditOrganization.aspx?orgId=" + orgId);

        }

        protected void ClickColumn(object sender, EventArgs e)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "confirm('You clicked YES!')", true);
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
            }
        }
    }
}
